import { Component, OnInit, ViewChild, ViewContainerRef, Renderer2, TemplateRef } from '@angular/core';
import { HttpHelper } from '../../../Helper/HttpHelper';
import { SmartTableDataSource } from '../../../Helper/SmartTableDataSource';
import { Fun } from '../../../Config/Fun';
import { DtoResultObj } from '../../../Model/DtoRec/DtoResult';
import { NbDialogService, NbWindowService } from '@nebular/theme';
import { ServerSourceConf } from '../../../Lib/ng2-smart-table/lib/lib/data-source/server/server-source.conf';
import { Variables } from '../../../../../src/app/Config/Variables';

@Component({
  selector: 'wzl-course-look',
  templateUrl: './course-look.html',
})
export class LookComponent implements OnInit {
  bean: any;
  classes_id = 0;

  constructor(
    public httpHelper: HttpHelper,
    private dialogService: NbDialogService
  ) {

  }

  ngOnInit() {

  }

  myModelChange(val) {
    if (this.classes_id != val) {
      this.classes_id = val;
      console.log(this.classes_id);
      this.httpHelper.Post("WeekCourse/GetClassesCourse", { Key: this.classes_id }).then((result: DtoResultObj<string>) => {
        Fun.HideLoading();
        if (result.success) {
          this.bean = result.data;

          console.log(this.bean)
        }
        else {
          Fun.Hint(result.msg);
        }
        console.log(result);
      })
    }
  }

  openObj;
  changeList: Array<any>;
  clickCourse(id, dialog: TemplateRef<any>) {
    if (this.changeList == null) this.changeList = [];
    if (this.changeList.length > 1) {
      this.changeList.splice(0, 1);
    }
    console.log(this.bean.courseDic[id])
    if (this.bean.courseDic[id].order_num == 0) {
      this.bean.courseDic[id].classes_name = this.bean.name;
      this.bean.courseDic[id].order_num = parseInt(id.replace('a', ''));
    }
    this.changeList.push(this.bean.courseDic[id]);

  }

  /**
   * 交换课程
   */
  ChangeCourse() {
    let postEnt = {
      Key: []
    }
    this.changeList.forEach(element => {
      console.log(element.order_num)
      var tmp1 = element.order_num / 10;
      var tmp2 = element.order_num % 10;
      postEnt.Key.push({
        k: element.classes_name,
        v: tmp2 + "" + Math.floor(tmp1)
      });
    });

    console.log(postEnt);
    this.httpHelper.Post("WeekCourseChange/ChangeWeekCourse", postEnt).then((result: DtoResultObj<string>) => {
      Fun.HideLoading();
      if (result.success) {
        var tmp = this.classes_id;
        this.classes_id = 0;
        this.myModelChange(tmp)
      }
      else {
        Fun.Hint(result.msg);
      }
      console.log(result);
    })
  }
}
